//
//  ProductModel.swift
//
//  Created by eme on 2016/12/17
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import NetWorkCore
import ModelProtocol

public class ProductModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kProductPriceTypeKey: String = "price_type"
    internal let kProductImgKey: String = "img"
    internal let kProductPdnameKey: String = "pdname"
    internal let kProductPriceKey: String = "price"
    internal let kProductPriceMinKey: String = "price_min"
    internal let kProductPdidKey: String = "pdid"
    internal let kProductSkusKey: String = "skus"
    internal let kProductPriceMaxKey: String = "price_max"
    internal let kProductSkuTypeKey: String = "sku_type"
    
    
    // MARK: 属性
    public var priceType: Int
    public var img: String
    public var pdname: String
    public var price: Double
    public var priceMin: Double
    public var pdid: Int
    public var skus: [SkusModel]
    public var priceMax: Double
    public var skuType: Int
    public var unit:String
    public var brid:String
    public var skuONE:SkusModel
    public var stock:Int
    public var pdstate:String
    public var cid:String
    public var agmobile:String
    public var buy:Int
    public var istimeby:String
    public var mixmoq:Int
    
    ///是否启用混批
    public var isHp:Bool = false{
        didSet{
            /*
             //sku_type = 1 只有一个规格，取skuOne，此时商品的skus是空的,skuOne的标准属性除了skus是空的以外，其他都有值
             //sku_type=2 一维规格数组，取skus，此时商品的skuOne是空的，skus中的每个元素除了skus是空的以外，其他都有值
             //sky_type=3 二维规格数组，取skus，此时skuOne是空的，skus中的每个元素仅skuname和skus有值，每个元素的skus又是一个数组，每个元素除了skus以外都是有值
             */
            if self.skuType == 2 {
                for skuModel in self.skus{
                    skuModel.isHp = isHp
                }
            }else if self.skuType == 3{
                for skuModel in self.skus{
                    for subSkuModel in skuModel.skus{
                        subSkuModel.isHp = isHp
                    }
                }
            }
        }
    }
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    public required  init?(json: JSON) {
        priceType = json[kProductPriceTypeKey].intValue
        img = json[kProductImgKey].stringValue
        pdname = json[kProductPdnameKey].stringValue
        price = json[kProductPriceKey].doubleValue
        priceMin = json[kProductPriceMinKey].doubleValue
        pdid = json[kProductPdidKey].intValue
        agmobile = json["agmobile"].stringValue
        buy = json["buy"].intValue
        istimeby = json["istimeby"].stringValue
        skus = []
        if let items = json[kProductSkusKey].array {
            for item in items {
                if let skusModel = SkusModel(json: item){
                    skus.append(skusModel)
                }
            }
        } else {
            skus = []
        }
        priceMax = json[kProductPriceMaxKey].doubleValue
        skuType = json[kProductSkuTypeKey].intValue
        unit = json["unit"].stringValue
        brid = json["brid"].stringValue
        pdstate = json["pdstate"].stringValue
        stock = json["stock"].intValue
        cid = json["cid"].stringValue
        skuONE = SkusModel(json: json["skuONE"])!
        // APP商品详情点击立即订购弹出界面中默认值修改，单sku商品时默认值为1(APP-IOS端)
        skuONE.editnum = 1
        mixmoq = json["mixmoq"].intValue
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    public  func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        dictionary.updateValue(priceType as AnyObject, forKey: kProductPriceTypeKey)
        dictionary.updateValue(img as AnyObject, forKey: kProductImgKey)
        dictionary.updateValue(pdname as AnyObject, forKey: kProductPdnameKey)
        dictionary.updateValue(price as AnyObject, forKey: kProductPriceKey)
        dictionary.updateValue(priceMin as AnyObject, forKey: kProductPriceMinKey)
        dictionary.updateValue(pdid as AnyObject, forKey: kProductPdidKey)
        if skus.count > 0 {
            var temp: [AnyObject] = []
            for item in skus {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kProductSkusKey)
        }
        dictionary.updateValue(priceMax as AnyObject, forKey: kProductPriceMaxKey)
        dictionary.updateValue(skuType as AnyObject, forKey: kProductSkuTypeKey)
        
        return dictionary
    }
    
    
}
